* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file runs additional regressions for domestic market outcomes with the synthetic merger control group.

*** Products per market, only in markets that exported before
*** Create dataset of exports for all firms
use "D:\Data\Workdata\703989\fjsd3989\STATA FILES\uhdi_subset.dta", clear
rename cvrnr cvrnr_uhdi
keep if ie==2
sort senr aar 
merge m:1 senr aar using "D:\Data\Workdata\703989\fjsd3989\STATA FILES\se_cvr_yearly.dta" 

* Compare cvrnr_uhdi with cvrnr in se-cvrnr key file
gen temp=0
replace temp=1 if cvrnr == cvrnr_uhdi & _merge==3
tab temp if _merge==3

keep if _merge==3
drop temp cvrnr_uhdi senr _merge

drop enhedskode ie mgd statistikkode vgt

order cvrnr aar vare land
sort cvrnr aar vare land

rename cvrnr cvrnrA
rename vare vareA
rename aar aarA
rename land landA
rename vrd vrdA

gen byte notnumeric=real(vareA)==.
tab notnumeric
drop if notnumeric==1
destring vareA, replace
rename vareA cn8A
drop if vrdA<=0

sort cvrnrA aarA cn8A landA
by cvrnrA aarA cn8A landA: gen _prod_distinct = (cn8A~=cn8A[_n-1])
egen nprod = sum(_prod_distinct), by(cvrnrA aarA landA)
drop _prod_distinct
save "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_uhdi_subset_cn8.dta", replace

foreach v in 3c 4c 9c{

if "`v'"=="3c"{
	local table "A13"	
	}
if "`v'"=="4c"{
	local table "figureA6"	
	}	
if "`v'"=="9c"{
	local table "figureA7"	
	}	
	
* From Do File #R16
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exp_mergers_synthetic_02102023_v`v'.dta", clear 
keep cvrnrA aar
duplicates drop
sort cvrnrA aar
by cvrnrA: gen dup = _n
sum dup
local noyears `r(max)'
reshape wide aar, i(cvrnrA) j(dup)
tempfile temp_aar_wide_did1865
save `temp_aar_wide_did1865.dta', replace

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_uhdi_subset_cn8.dta", clear
collapse (sum) vrdA (mean) nprod, by(cvrnrA aarA landA)
merge m:1 cvrnrA using `temp_aar_wide_did1865.dta', keep(match) nogen
reshape long aar, i(cvrnrA aarA landA vrdA nprod) j()
drop if aar==.

gen d_merger_y = (aarA - aar)
egen group = group(cvrnrA aar landA)
xtset group aarA
gen LvrdA = L.vrdA
gen L2vrdA = L2.vrdA
gen L3vrdA = L3.vrdA
gen L4vrdA = L4.vrdA
egen LsumvrdA = rowtotal(LvrdA L2vrdA L3vrdA L4vrdA)
gen p1_pos = (LsumvrdA>0 & d_merger_y==1)

drop LsumvrdA
gen L5vrdA = L5.vrdA
egen LsumvrdA = rowtotal(L2vrdA L3vrdA L4vrdA L5vrdA)
gen p2_pos = (LsumvrdA>0 & d_merger_y==2)

drop LsumvrdA
gen L6vrdA = L6.vrdA
egen LsumvrdA = rowtotal(L3vrdA L4vrdA L5vrdA L6vrdA)
gen p3_pos = (LsumvrdA>0 & d_merger_y==3)

gen exported_before = (p1_pos==1 | p2_pos==1 | p3_pos==1)
egen maxexported_before = max(exported_before), by(group)
keep if maxexported_before==1

rename nprod nprodpermark
gen maxnprodpermark = nprodpermark
collapse (max) maxnprodpermark (mean) nprodpermark, by(cvrnrA aar aarA) 
gen lnnprodpermark = ln(nprodpermark)
gen lnmaxnprodpermark = ln(maxnprodpermark)
tempfile temp_exports_nprodpermark_expb4
save `temp_exports_nprodpermark_expb4.dta', replace	

********************************************************************************

* From Do File #R17
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_did_imputation_export_prepare_jc_synthetic_02102023_v`v'.dta", clear

rename cvrnrA cvrnr
rename aar aar_ori
rename aarA aar
merge 1:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", keep(match master) nogen
rename aar aarA
rename aar_ori aar
rename cvrnr cvrnrA

* From Do File #P11
merge 1:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_cvrnr_dist_v2.dta", keep(match master) gen(_mergedist)

* From Do File #P5
merge 1:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_nprodpermark.dta", keep(match master) nogen

rename lnnprodpermark lnanprodpermark
rename lnmaxnprodpermark lnamaxnprodpermark

gen nproddivnmark = nprod/nmark
gen lnnproddivnmark = ln(nproddivnmark)

* Drop if merger event occurs before 1996 (not enough years for pre-merger period)
drop if aar<1996 & acquirer==1

* Drop if acquirer has no merger event in remaining sample
egen meanaar = mean(aar), by(cvrnrA)
drop if meanaar==. & acquirer==1

* Drop if acquirer has multiple merger events
egen sdaar = sd(aar), by(cvrnrA)
drop if sdaar>0 & sdaar!=.

* Calculate minimum number of employees in sample
egen minemployee_cvrnr = min(employee_cvrnr) if nprod>0 & nprod!=., by(cvrnrA)

* Additional calculations
replace d_yr = aarA - meanaar

* From above
rename aar aar_ori
rename meanaar aar
merge 1:1 cvrnrA aar aarA using `temp_exports_nprodpermark_expb4.dta', keep(match master)
egen max_merge = max(_merge), by(cvrnrA)
drop _merge

rename aar meanaar
rename aar_ori aar

qui tab aarA, gen(D_AA_)
qui tab aar, gen(D_A_)

* Define spells for imputation estimator
gsort cvrnrA -aarA
by cvrnrA: carryforward aar, gen(aar2)
sort cvrnrA aarA
by cvrnrA: carryforward aar2, gen(aar3)
replace aar3 = aar3+1

gen p4 = (d_yr>3)
gen m4 = (d_yr<-3)

foreach x in p4 p3 p2 p1 m1 m2 m3 m4{
replace `x' = 0 if acquirer==0
}

foreach i of numlist 1(1)3{
gen M`i'=0
replace M`i'=1 if d_yr==-`i'
}

foreach i of numlist 1(1)3 {
gen P`i'=0
replace P`i'=1 if d_yr==`i'
}

gen P4 = (d_yr>3)
gen M4 = (d_yr<-3)

foreach x in P4 P3 P2 P1 M1 M2 M3 M4{
gen `x'Xa = `x'*acquirer
}

gen post = (d_yr>0)
gen postXa = post*acquirer

foreach x in lnnmark lnnprod {
areg `x' P4 P3 P2 P1 test M1 M2 M3 M4 P4Xa P3Xa P2Xa P1Xa M1Xa M2Xa M3Xa M4Xa D_AA_1-D_AA_25 if minemployee_cvrnr>=5 & _mergedist==3, absorb(cvrnrA) cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_`table'.txt", keep(P4 P3 P2 P1 test M1 M2 M3 M4 P4Xa P3Xa P2Xa P1Xa M1Xa M2Xa M3Xa M4Xa D_AA_1-D_AA_25) bdec(4) sdec(4) nocons nonotes
}

foreach x in lnnprodpermark {
areg `x' P4 P3 P2 P1 test M1 M2 M3 M4 P4Xa P3Xa P2Xa P1Xa M1Xa M2Xa M3Xa M4Xa D_AA_1-D_AA_25 if minemployee_cvrnr>=5 & _mergedist==3 & max_merge==3, absorb(cvrnrA) cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_`table'.txt", keep(P4 P3 P2 P1 test M1 M2 M3 M4 P4Xa P3Xa P2Xa P1Xa M1Xa M2Xa M3Xa M4Xa D_AA_1-D_AA_25) bdec(4) sdec(4) nocons nonotes
}

foreach x in p90_ldistw {
areg `x' P4 P3 P2 P1 test M1 M2 M3 M4 P4Xa P3Xa P2Xa P1Xa M1Xa M2Xa M3Xa M4Xa D_AA_1-D_AA_25 if minemployee_cvrnr>=5 & _mergedist==3, absorb(cvrnrA) cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_`table'.txt", keep(P4 P3 P2 P1 test M1 M2 M3 M4 P4Xa P3Xa P2Xa P1Xa M1Xa M2Xa M3Xa M4Xa D_AA_1-D_AA_25) bdec(4) sdec(4) nocons nonotes
}

if "`v'"=="3c"{
foreach x in p90_lr_ltradingimportdocs p90_lr_ltradingimporttime p90_lr_ltradingimportcost_def {
areg `x' P4 P3 P2 P1 test M1 M2 M3 M4 P4Xa P3Xa P2Xa P1Xa M1Xa M2Xa M3Xa M4Xa D_AA_1-D_AA_25 if minemployee_cvrnr>=5 & _mergedist==3, absorb(cvrnrA) cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_figureA8.txt", keep(P4 P3 P2 P1 test M1 M2 M3 M4 P4Xa P3Xa P2Xa P1Xa M1Xa M2Xa M3Xa M4Xa D_AA_1-D_AA_25) bdec(4) sdec(4) nocons nonotes
}
}

}
